package org.linlinjava.litemall.wx.manage;

import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.notify.AwsNotifyService;
import org.linlinjava.litemall.core.notify.NoticeHelper;
import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LumiereSubscribeUser;
import org.linlinjava.litemall.db.service.LumiereSubscribeService;
import org.linlinjava.litemall.wx.constant.LitemallWXConstants;
import org.linlinjava.litemall.wx.dto.SubscribeConfirmDTO;
import org.springframework.stereotype.Service;
import static org.linlinjava.litemall.wx.util.WxResponseCode.MOBILE_CODE_ONE;
@Service
@RequiredArgsConstructor
public class WxNewSubscribeManageService {

    private final NoticeHelper noticeHelper;

    private final AwsNotifyService awsNotifyService;

    /**
     * 存在用户发送通知
     */
    public void sendNotificationToExistUser(SubscribeConfirmDTO dto, LitemallUser user, LitemallUser giveeUser, LumiereSubscribeUser lumiereSubscribeUser){
        /** 赠送给有账号的用户 */
        //给自己通知
        noticeHelper.noticeUser(Constants.MSG_TYPE_OTHER, LitemallWXConstants.WX_SUBSCRIPTION_USER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_SUBSCRIPTION_USER_NOTICEHELPER,giveeUser.getUsername())
                ,user.getId(),lumiereSubscribeUser);
        awsNotifyService.sendSms(MOBILE_CODE_ONE+user.getMobile(),
                String.format(LitemallWXConstants.WX_SUBSCRIPTION_USER_NOTICEHELPER,giveeUser.getUsername())
                , Constants.AWS_MESSAGE_TYPE_TRANSACTIONAL);
        /**给被赠送者通知*/
        //给他领取链接
        noticeHelper.noticeUser(Constants.MSG_TYPE_OTHER,LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_NOTICEHELPER,user.getNickname(),user.getUsername(),user.getUsername(),dto.getMessage())
                , giveeUser.getId(), lumiereSubscribeUser);
        //短信通知
        if(giveeUser.getMobile() != null){
            awsNotifyService.sendSms(MOBILE_CODE_ONE+giveeUser.getMobile(),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_SENDSMS,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_GETTHELINK,user.getUsername(),dto.getMessage())
                    , Constants.AWS_MESSAGE_TYPE_TRANSACTIONAL);
        }
        //邮箱通知
        if(giveeUser.getEmail() != null ){
            awsNotifyService.noticeMail(String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_TITLE_NOTICEMAIL,user.getNickname(),user.getUsername()),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_NOTICEMAIL,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_GETTHELINK,user.getUsername(),dto.getMessage()),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_NOTICEMAIL,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_GETTHELINK,user.getUsername(),dto.getMessage())
                    ,giveeUser.getEmail() );
        }
    }


    /**
     * 不存在用户发送通知
     */
    public void sendNotificationToNotExistUser(SubscribeConfirmDTO dto, LitemallUser user, LumiereSubscribeUser lumiereSubscribeUser){
        /** 赠送给没有账号的用户 */
        //给自己通知
        noticeHelper.noticeUser(Constants.MSG_TYPE_OTHER,LitemallWXConstants.WX_SUBSCRIPTION_USER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_SUBSCRIPTION_USER_NOTICEHELPER,dto.getGivee())
                ,user.getId(),lumiereSubscribeUser);
        awsNotifyService.sendSms(MOBILE_CODE_ONE+user.getMobile(),
                String.format(LitemallWXConstants.WX_SUBSCRIPTION_USER_NOTICEHELPER,dto.getGivee())
                , Constants.AWS_MESSAGE_TYPE_TRANSACTIONAL);
        /**给被赠送者发通知*/
        //短信通知
        if(RegexUtil.isMobileValid(dto.getGivee())){
            awsNotifyService.sendSms(MOBILE_CODE_ONE+dto.getGivee(),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_SENDSMS,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_DOWNLOADLINK,user.getUsername(),dto.getMessage())
                    , Constants.AWS_MESSAGE_TYPE_TRANSACTIONAL);
        }
        //邮箱通知
        //邮箱通知
        if(dto.getGivee() != null ){
            awsNotifyService.noticeMail(String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_TITLE_NOTICEMAIL,user.getNickname(),user.getUsername()),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_NOTICEMAIL,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_GETTHELINK,user.getUsername(),dto.getMessage()),
                    String.format(LitemallWXConstants.WX_SUBSCRIPTION_GIVEEUSER_NOTICEMAIL,user.getNickname(),user.getUsername(),LitemallWXConstants.WX_SUBSCRIPTION_GETTHELINK,user.getUsername(),dto.getMessage())
                    ,dto.getGivee() );
        }
    }

}
